<template>
	<view class="column">
		<!--  -->
		<ben></ben>
		<!--  -->
		<!-- 背景 -->
		<view class="posabs zin-1 wth100">
			<view class="top">
			</view>
			<view class="top-header">
			</view>
		</view>
		<!-- 背景 -->
		<!-- 定位 -->
		<view class="positioning disflex padleft30">
			<image class="location" src="/static/image/location.png"></image>
			<text style="color: #fff;">{{ add }}</text>
			<!-- <u-icon name="arrow-down-fill" color="#FFFFFF" size="14"></u-icon> -->
		</view>
		<!-- 定位 -->
		<!-- 搜索框 -->
		<view class="min30">
			<u-search placeholder="请输入商品名搜索" v-model="search" actionText="搜索"
				@custom="$jump.nav('/indexpackage/user/search?search=' + search)"
				@search="$jump.nav('/indexpackage/user/search?search=' + search)" :showAction="true" :animation="true"
				height="30"></u-search>
		</view>
		<!-- 搜索框 -->
		<!-- 轮播 -->
		<view class="min30_rl">
			<u-swiper class="swiper-img" :list="swiperlist" keyName="image" height="180" indicator='true'
				indicator-mode="dot"></u-swiper>
		</view>
		<!-- 轮播 -->
		<!-- 菜单 -->
		<view class="mintop30">
			<u-grid :border="false" col="4" align="center ">
				<u-grid-item v-for="(listItem,listIndex) in info" :key="listIndex"
					@click="navClassify(listItem.title,listItem.id)">
					<image class="menu-icon" :src="listItem.icon" />
					<text class="size28 col0 minbottom20">{{listItem.title}}</text>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- 菜单 -->
		<!-- 会员 -->
		<view class="min30_rl" @click="$jump.nav('/mypackage/user/recharge_integral')">
			<image class="advertising-img" src="/static/image/Frame.png"></image>
		</view>
		<!-- 会员 -->
		<!-- 推荐门店 -->
		<view class="box rad2 pad20 min30">
			<view class="recommend row minbottom20">
				<image class="recommend-stores" src="../../static/image/Vector.png"></image>
				<view class="disflex align_item_c" @click="$jump.nav('/indexpackage/user/store_list')">
					<text class="text-content padleft20 size28">查看更多门店</text>
					<image class="arrowhead padleft20" src="/static/image/111.png"></image>
				</view>
			</view>
			<view class="background_f rad2 column pad20">
				<view class="row minbottom20">
					<image class="shop-avaturl rad2" :src="recommend.stores_logo"></image>
					<view class="column minleft20" style="width: 79%;">
						<view class="row align_item_c">
							<text class="col3 size28 font600 flex1">{{ recommend.stores_name }}</text>
							<view class="row minau minright0"
								@click="$jump.nav('/indexpackage/user/store_info?stores_id=' + recommend.id)">
								<text class="col9 size24 font500" @click="btn_stores">进入店铺</text>
								<u-icon name="arrow-right" color="#999" size="12"></u-icon>
							</view>
						</view>
						<text class="minto minbottom0 size24 font500 col9">地址：{{ recommend.stores_address }}</text>
					</view>
				</view>

				<view class="row">
					<image class="goods-img rad2" v-for="(item, index) in recommend.product" :key="index"
						:src="item.image">
					</image>
				</view>
			</view>
		</view>
		<view class="minauto text_center">
			<text class="font_italic size36 font600">商品推荐</text>
			<view class="underline"></view>
		</view>
		<view class="row min30_rl mintop30 align_item_c">
			<view class="rad3 size26 col0 font500" style="padding: 6rpx 30rpx;"
				:style="siftIndex == siftindex ? 'background-color: #EBEBEB;' : ''" v-for="(siftInfo,siftindex) in sift"
				@click="tabSift(siftindex)">
				{{ siftInfo }}
			</view>
			<view class="minau minright0 size26 col0 font500 row" @click="show = true">
				<u-icon name="list" color="#FE9110" size="18" bold="true"></u-icon>
				{{classify ? classify : '商品分类'}}
			</view>
		</view>
		<!-- 列表 -->
		<view class="empty" style="margin-top: 100rpx;padding-bottom: 100rpx;" v-if="!loadmore">
			<u-empty mode="data" icon="https://cdn.uviewui.com/uview/empty/data.png">
			</u-empty>
		</view>
		<view class="min30_rl minbottom20" v-else>
			<view class="row flexwrap">
				<view class="rad2 list column mintop30"
					style="box-shadow: 1rpx 1rpx 8rpx 2rpx rgba(206,188,172,0.24);width: 48%;"
					v-for="(item,index) in goodsArr" @click="$jump.nav('/indexpackage/user/goods_info?id=' + item.id)">
					<image :src="item.image" mode=""
						style="width: 330rpx;height: 330rpx;border-radius: 20rpx 20rpx 0 0;"></image>
					<view class="column min20 flex1">
						<view class="size26 col3 font500 omit2">
							{{ item.title }}
						</view>
						<view style="margin-top: auto;margin-bottom: 0;">
							<view class="row">
								<view class="size22 font500 row align_item_c" style="color: #C0C0C0;">月销量
									<view class="">
										{{ item.sales }}
									</view>
								</view>
								<view class="minau minright0 row">
									<view class="size36 font600" style="color: #FE3C3C;">
										<text>￥</text>
										<text>{{item.specs[0].selling_price}}</text>
									</view>
								</view>
							</view>
							<view class="row mintop20 align">
								<image :src="item.shop.stores_logo" class="rad50" style="width: 34rpx;height: 34rpx;">
								</image>
								<view class="font500 size24 minleft10 omit1 flex1" style="color: #383838;">
									{{ item.shop.stores_name }}
								</view>
								<u-icon name="arrow-right" color="#333" size="14"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view style="margin-top: 50rpx;"><u-loadmore :status="status" /></view>
		</view>
		<button type="button" class="weixin-btn" open-type="chooseAvatar" @chooseavatar="chooseavatar"></button>
		<!-- 列表 -->
		<!-- 商品分类 -->
		<u-picker :show="show" :columns="columns" keyName="title" @confirm="confirm" @cancel="cancel"></u-picker>
		<!-- 商品分类 -->
		<!-- 底部tab -->
		<block>
			<userTab :item="0"></userTab>
		</block>
		<!-- 底部tab -->
	</view>
</template>

<script>
	import userTab from "@/components/tabBar/userTab.vue"
	import QQMapWX from "@/tool/qqmap-wx-jssdk.js"
	export default {
		components: {
			userTab,
		},
		data() {
			return {
				add: '',
				order: '',
				classify_id: '',
				page: 1,
				show: false,
				
				value1: 0,
				siftIndex: 0,
				classify: '',
				search: '',
				status: 'nomore',
				loadmore: [],
				swiperlist: [],
				recommend: {},
				info: [],
				goodsArr: [],
				shop_goods_img: [{
						"img": "/static/goods/6t0m2Pod.jpg"
					},
					{
						"img": "/static/goods/9qXr2Hs4.jpg"
					},
					{
						"img": "/static/goods/PQoxO8Fg.jpg"
					},
					{
						"img": "/static/goods/r1UlrOmF.jpg"
					},
				],
				sift: ['综合', '最新', '价格', '销量'],
				columns: [
					[]
				],
			}
		},

		onLoad() {
			const user_type = uni.getStorageSync('user_type');
			this.page = 1
			this.getProductList()
			if (user_type == 2) {
				console.log(user_type)
				uni.redirectTo({
					url: '/pages/store/index',
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.log(err);
					}
				});
			} else {
				uni.switchTab({
					url: '/pages/user/index',
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.log(err);
					}
				});
			}
			uni.hideTabBar({
				animation: true, // 是否需要动画效果
				success: function() {
					console.log('TabBar has been hidden successfully');
				},
				fail: function(err) {
					console.error('Failed to hide TabBar:', err);
				}
			});
		},
		onShow() {
			const user_type = uni.getStorageSync('user_type');

			if (user_type == 2) {
				console.log(user_type)
				uni.switchTab({
					url: '/pages/store/index',
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.log(err);
					}
				});
				return
			}


			this.getSwiper()
			this.getMenu()
			this.getClassify()
			this.getLocationInfo()
			this.getRecommend()
		},
		onReachBottom() {
			if (this.page < 0) return;
			this.status = 'loading';
			this.page = ++this.page
			this.getProductList()
			setTimeout(() => {
				console.log(this.loadmore);
				if (this.loadmore.length == 0) this.status = 'nomore';
				else this.status = 'loading';
			}, 1000)
		},
		methods: {
			// 获取位置信息
			getLocationInfo() {
				//获取位置信息
				var _this = this
				let location = {
					longitude: 0,
					latitude: 0,
					province: "",
					city: "",
					area: "",
					street: "",
					address: "",
				}
				uni.getLocation({
					type: "gcj02",
					success(res) {
						location.longitude = res.longitude;
						location.latitude = res.latitude;
						// 腾讯地图Api
						const qqmapsdk = new QQMapWX({
							key: 'V6CBZ-5K4CX-3WX4O-Z2BWQ-SXHMJ-UPB6C' //这里填写自己申请的key
						});
						// 使用腾讯地图api
						qqmapsdk.reverseGeocoder({
							location,
							success(response) {
								let info = response.result;
								// 注意查看打印信息
								console.log('获取位置信息', info);
								_this.add = info.address_component.city

							},
						});
					},
					fail(err) {
						console.log(err)
					},
				});
			},
			async getRecommend() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("home/recommend")
				if (code == 200) {
					this.recommend = data
				} else {
					console.log(message);
				}
			},
			async getSwiper() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("home/barer")
				if (code == 200) {
					this.swiperlist = data
				} else {
					console.log(message);
				}
			},
			async getMenu() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("home/menu_list")
				if (code == 200) {
					this.info = data

				} else {
					console.log(message);
				}
			},
			async getClassify() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("home/home_classify")
				if (code == 200) {
					this.columns[0] = data
				} else {
					console.log(message);
				}
			},
			async getProductList() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("home/product_list", {
					order: this.siftIndex == 0 ? this.classify_id ? 4 : '' : this.siftIndex,
					classify_id: this.classify_id,
					page: this.page
				})
				if (code == 200) {
					if (this.page != 1) {
						this.loadmore = data.items
						for (var i = 0; i < data.items.length; i++) {
							this.goodsArr.push(data.items[i]); // 加载的数据追加在上一页后面
						}
					} else {
						this.loadmore = data.items
						this.goodsArr = data.items
					}
				} else if (code == 500) {
					this.goodsArr = data
					this.loadmore = data
				} else {
					console.log(message);
				}
			},
			navClassify(title, id) {
				if (title != '全部分类') {
					uni.setStorageSync('classify_id', id);
				}
				uni.switchTab({
					url: '/pages/user/classify'
				})
			},
			tabSift(i) {
				this.siftIndex = i
				this.classify = ''
				this.classify_id = ''
				this.page = 1
				this.getProductList()
			},
			btn_recommend_stores() {
				console.log('点击了查看更多门店')
			},
			btn_stores() {
				console.log('点击了进入门店')
			},
			cancel() {
				this.show = false
				this.classify = ''
				this.classify_id = ''
			},
			confirm(e) {
				console.log(e);
				this.page = 1
				this.classify = e.value[0].title
				this.classify_id = e.value[0].id
				this.siftIndex = 0
				// if (this.siftIndex == 0) {
				// 	this.siftIndex = 4
				// }
				this.show = false
				this.getProductList()
			}
		}
	}
</script>

<style lang="less" scoped>
	.top {

		height: 250rpx;
		background-image: linear-gradient(120deg, #ff5a58 0%, #ff7e3f 100%);
	}

	.top-header {
		height: 300rpx;
		background-image: linear-gradient(120deg, #ff5a58 0%, #ff7e3f 100%);
		border-bottom-left-radius: 50%;
		border-bottom-right-radius: 50%;
	}

	.positioning {
		align-items: flex-end;
		margin-top: 100rpx;

		.location {
			width: 50rpx;
			height: 48rpx;
		}
	}

	.menu-icon {
		width: 120rpx;
		height: 120rpx;
		border-radius: 55%;
	}

	.advertising-img {
		width: 100%;
		height: 260rpx;
	}

	.list {
		&:nth-child(2n) {
			margin-left: 26rpx;
		}
	}

	.box {
		background-image: linear-gradient(120deg, #ffd29b 0%, #ffebbb 100%);

		.recommend {
			/* 垂直居中对齐 */
			justify-content: space-between;
		}

		.recommend-stores {
			width: 180rpx;
			/* 设置图片宽度 */
			height: 40rpx;
			/* 设置图片高度 */
		}

		.text-content {
			color: #ff5925;

		}

		.arrowhead {
			width: 50rpx;
			height: 24rpx;

		}

		.shop-avaturl {
			width: 18%;
			height: 110rpx;
		}

		.goods-img {
			width: 140rpx;
			height: 140rpx;
			margin-left: 3%;

			&:nth-child(1) {
				margin: 0;
			}
		}
	}

	.underline {
		width: 200rpx;
		/* 下划线宽度占满整个视图宽度 */
		height: 10rpx;
		/* 下划线高度 */
		background-color: #fe9110;
		border-radius: 10rpx;
	}
</style>